package com.fire8.dolphin.ui;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.fire8.dolphin.service.GetDiaryForUser;
import com.fire8.dolphin.ui.diary.DiaryListActivity;
import com.fire8.dolphin.ui.fm.FMActivity;
import com.fire8.dolphin.ui.more.HelpActivity;
import com.fire8.dolphin.ui.more.MoreActivity;
import com.fire8.dolphin.ui.plan.PlanListActivity;
import com.fire8.dolphin.utils.LogUtil;

public class MainActivity extends TabActivity implements
OnCheckedChangeListener {
    private TabHost mHost;
    private Intent mHomeIntent;//首页
    private Intent mMsgIntent;//信息
    private Intent mTopicIntent;//话题
    //private Intent mFriendIntent;//好友
    private Intent mMoreIntent;//更多
    private ImageView imgMsg;//显示新消息图标
    //private RadioButton btnMsg;
    private RadioGroup radioGroup;
    private static boolean openFlag=false;
    private DolphinApplication app;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maintabs);
        app = (DolphinApplication)getApplication();
        //mHost = (TabHost) findViewById(R.id.);
        mHomeIntent = new Intent(this,FMActivity.class);//第一页，显示电台
        //mMsgIntent = new Intent(this,MsgTabActivity.class);
        mMsgIntent = new Intent(this,DiaryListActivity.class);//第二页，能量广场
        mTopicIntent = new Intent(this,PlanListActivity.class);//第四页，康复计划
        //mFriendIntent = new Intent(this,FriendTabActivity.class);//第三页，心理咨询
        mMoreIntent = new Intent(this,MoreActivity.class);
        //btnMsg = (RadioButton) findViewById(R.id.radio_button2);
        imgMsg = new ImageView(MainActivity.this);
        imgMsg.setId(100001);
        imgMsg.setImageResource(R.drawable.msg);
        //imgMsg.setVisibility(View.GONE);
        FrameLayout.LayoutParams imgLp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        imgLp.gravity = Gravity.RIGHT | Gravity.TOP;
        imgLp.setMargins(0, 1, 1, 0);
        imgMsg.setLayoutParams(imgLp);
        
        IntentFilter filter = new IntentFilter("com.fire8.dolphin.ui.ChatActivity");
        this.registerReceiver(mReceiver, filter);
        
        initRadios();
        setupIntent();
        LogUtil.d("onCreate");
        if(app.isFirst())
        {
          app.first(false);
          //启动使用向导，在向导里修改使用次数
          Intent intent = new Intent(this,HelpActivity.class);
          startActivity(intent);
          //启动异步任务，下载新手须知
          //LogUtil.i("启动异步任务，下载新手须知");
          new GetDiaryForUser(this).execute("");
        }       
    }
    /**
     * 初始化底部按钮
     */
    private void initRadios() {
         ((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this);
         //btnMsg.setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(this);
         radioGroup = (RadioGroup)findViewById(R.id.main_radio);
    }
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        
          if (isChecked) {
              switch (buttonView.getId()) {
                  case R.id.radio_button0:
                      this.mHost.setCurrentTabByTag("tab1");
                      //btnMsg.setChecked(false);
                      break;
                  case R.id.radio_button1:
                      this.mHost.setCurrentTabByTag("tab2");
                      //btnMsg.setChecked(false);
                      break;
//                  case R.id.radio_button2:
//                      this.mHost.setCurrentTabByTag("tab3");
//                      radioGroup.clearCheck();
//                      imgMsg.setVisibility(View.GONE);
//                      break;
                  case R.id.radio_button3:
                      this.mHost.setCurrentTabByTag("tab4");
                      //btnMsg.setChecked(false);
                      break;
                  case R.id.radio_button4:
                      this.mHost.setCurrentTabByTag("tab5");
                      //btnMsg.setChecked(false);
                      break;
              }
          }
    }
    private void setupIntent() {
      this.mHost = getTabHost();
      
      TabHost localTabHost = this.mHost;

      localTabHost.addTab(buildTabSpec("tab1", "bb",
              R.drawable.icon1, this.mHomeIntent));

      localTabHost.addTab(buildTabSpec("tab2", "aa",
              R.drawable.icon_msg, this.mMsgIntent));

//      localTabHost.addTab(buildTabSpec("tab3", "aa",
//              R.drawable.icon_haoyou, this.mFriendIntent));

      localTabHost.addTab(buildTabSpec("tab4", "aa",
              R.drawable.icon_huati, this.mTopicIntent));
      
      localTabHost.addTab(buildTabSpec("tab5", "aa",
              R.drawable.icon_gengduo, this.mMoreIntent));
      

  }
    @SuppressWarnings("unused")
    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
            final Intent content) {
        return this.mHost
                .newTabSpec(tag)
                .setIndicator(getString(resLabel),
                        getResources().getDrawable(resIcon))
                .setContent(content);
    }
    
    private TabHost.TabSpec buildTabSpec(String tag, String resLabel,
            int resIcon, final Intent content)
    {
        return this.mHost.newTabSpec(tag)
        .setIndicator(resLabel,
        getResources().getDrawable(resIcon))
        .setContent(content);
        
    }
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        //程序退出
        unregisterReceiver(mReceiver);
        LogUtil.d("程序退出");
    }
    @Override
    protected void onStart()
    {
        LogUtil.d("onStart");
        openFlag=true;
        super.onStart();
        //TODO 检查是否有新消息，如果有新消息，显示new图标
        Intent intent = new Intent();
        intent.setAction("com.fire8.dolphin.service.DolphinService");
        startService(intent);
        
    }
    /**
     * 显示新消息图标
     * <功能详细描述> [参数说明]
     *
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    private void showMsgImg()
    {
//        if(imgMsg.getParent() == null)
//        {
//            FrameLayout container = new FrameLayout(MainActivity.this);
//            container.setId(1000);
//            ViewGroup group = (ViewGroup)btnMsg.getParent();
//            int index = group.indexOfChild(btnMsg);
//            LayoutParams lp = btnMsg.getLayoutParams();
//            group.removeView(btnMsg);
//            group.addView(container, index, lp);
//            container.addView(btnMsg);
//            imgMsg.setVisibility(View.VISIBLE);
//            container.addView(imgMsg);
//            group.invalidate();
//        }else
//        {
//            imgMsg.setVisibility(View.VISIBLE);
//        }

    }
    @Override
    protected void onPause()
    {
        openFlag=false;
        super.onPause();
    }
    public static boolean isOpen()
    {
        return openFlag;
    }
    
    private final BroadcastReceiver mReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context arg0, Intent arg1)
        {
            //在会话按钮上显示new 标签
            showMsgImg();
        }
        
    };
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            LogUtil.d("mainActivity 按下了返回键");
        }
        return super.onKeyDown(keyCode, event);
    }
    
    
}